from django.http import QueryDict
from django.shortcuts import render,HttpResponse,redirect
from django.http import JsonResponse;
from django.http import HttpResponse


import requests,json
#http://127.0.0.1:8000/index?nem=ww
def index(request):
    #1、获取请求的方式 get/post/put
    print(request.method);
    #2、获取url中的参数
    quopri = request.GET
    print(quopri["nem"]+"ssss33");
    #return HttpResponse("欢迎访问")
    return redirect("https://www.baidu.com")

def user_list(request):
    return render(request,"user_list.html",{"title":"来了"});

def login(request):
    xiao_ming = {"name": "xiaoming", "age": 18}
    if request.method == "GET":
        print(xiao_ming.get("name"))
        return render(request,"login.html")

    #如果是post请求，获取用户请求的信息
    print(request.POST);
    username = request.POST["user"];
    password = request.POST["password"];
    print(username + password)
    if username == "root" and password == "root":
        return HttpResponse("登录成功");

    return render(request,"login.html",{"error_message":"用户名或者密码错误！"});

def test_json(request):
    xiao_ming = {"name": "xiaoming", "age": 18}
    return JsonResponse(xiao_ming);

def test_url(request):

    res = requests.get('http://172.254.242.133:8088/user/getUserByAge?age=20')
    print("-----------------")
    key = json.loads(res.text);
    return JsonResponse(key,safe=False);

